﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmDanToc : DevExpress.XtraEditors.XtraForm
    {

        #region Khai báo biến

        DanToc_BLL dt_bll = new DanToc_BLL();
        TextMessageBox box = new TextMessageBox();
        public string maDanToc = "";
        DataTable dt = new DataTable();
        private bool isOK = false;
        #endregion

        #region LoadForm

        public frmDanToc()
        {
            InitializeComponent();
        }

        private void frmDanToc_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region Hàm viết thêm

        private void LoadData()
        {
            if (maDanToc != "")
            {
                dt = dt_bll.TaoBang(box.sqlCheckExist("DM_DanToc","MaDanToc",maDanToc));
                DataRow dr = dt.Rows[0];
                txtMaDanToc.Text=dr["MaDanToc"].ToString();
                txtTenDanToc.Text = dr["TenDanToc"].ToString();
            }

        }
        private void SetInterface()
        { 
            maDanToc = "";
            txtMaDanToc.Text = "Mã tự sinh";
            txtTenDanToc.Text = "";
            txtTenDanToc.Focus();
        }
        private bool checkValid()
        {
            if (txtTenDanToc.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên dân tộc.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenDanToc.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = dt_bll.TaoBang(box.sqlCheckExist("DM_DanToc","TenDanToc",txtTenDanToc.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenDanToc.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenDanToc.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenUpdate()
        {
            dt = dt_bll.TaoBang(box.sqlCheckExist("DM_DanToc", "TenDanToc", txtTenDanToc.Text.Trim()));
            if (dt.Rows.Count +1> 1)
            {
                XtraMessageBox.Show(box.Exist(txtTenDanToc.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenDanToc.Focus();
                return false;
            }
            return true;
        }
        private void UpdateData()
        {
            if (checkValid())
            {
                DanToc_Public dt_public = new DanToc_Public();
                if (maDanToc == "")
                {
                    if (checkTenInsert())
                    {
                        dt_public.MaDanToc = box.AutoID("DT", 2, dt_bll.LayDuLieu());
                        dt_public.TenDanToc =box.toUpper(txtTenDanToc.Text);

                        dt_bll.Insert(dt_public, dt_public);
                        CloseForm();
                    }
                }
                else
                {
                    if (checkTenUpdate())
                    {
                        dt_public.MaDanToc = txtMaDanToc.Text.Trim();
                        dt_public.TenDanToc =box.toUpper(txtTenDanToc.Text);

                        dt_bll.Update(dt_public, dt_public);
                        CloseForm();
                    }
                }
            }
        }
        private void CloseForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        #endregion

        #region Các chức năng
        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
        #endregion

        #region Các sự kiện hệ thống
        private void frmDanToc_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK; 
            
        }
        #endregion
    }
}